package com.zl.zsk.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil<T> {
    @Autowired
    private RedisTemplate redisTemplate;

    public void set(String key,T t,long seconds){
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set(key,t,seconds,TimeUnit.SECONDS);
    }

    public void set(String key,T t,long time,TimeUnit timeUnit){
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set(key,t,time,timeUnit);
    }

    public boolean hasKey(String key){
        return redisTemplate.hasKey(key);
    }

    public void delete(String key){
        redisTemplate.delete(key);
    }

    public T get(String key){
        ValueOperations valueOperations = redisTemplate.opsForValue();
        T t = (T)valueOperations.get(key);
        return t;
    }

}
